package com.yq.homework.day24;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * @author 3307235355@qq.com
 * @since 2022/06/24 19:45
 * 递归查找指定目录中(包括子目录中)，
 * 所有的.java文件，并且，
 * 把所有这些找到的java文件，
 * 复制(是复制不是移动)到
 * 一个指定的目录下
 */
public class Demo04 {
    static String[] s1 = new String[10];
    static String[] s2 = new String[10];
    static int index = 0;
    static int count = 0;
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\app2");
        get(file);
        for (int i = 0; i < s1.length; i++) {
            if (s1[i] == null || s2[i] == null) {
                continue;
            }
            copy(s1[i], s2[i],"d:\\app2\\app3");
        }
    }

    public static void get(File file) {
        File[] files = file.listFiles();
        //找到java文件的个数
        for (File file1 : files) {
            if (file1.isFile()) {
                if (file1.toString().contains(".java")) {
                    s1[index++] = file1.getAbsolutePath();
                    s2[count++] = file1.getName();
                }
            } else {
                get(file1);
            }
        }
    }
    public static void copy(String fileAllName, String filename,String targetDir) throws IOException {
        FileInputStream in = new FileInputStream(fileAllName);
        String targetfile = targetDir + "\\" + filename;
        FileOutputStream out = new FileOutputStream(targetfile);
        byte[] bytes = new byte[1024];
        int readCount;
        while ((readCount = in.read(bytes)) != -1) {
            out.write(new String(bytes, 0, readCount).getBytes());
        }
        in.close();
        out.close();
    }
}
